package com.jyserver.common.tools;

import java.io.*;

public class Process {
    public static Object docmd(String[] command) {
        /**
         * @Author :ChenYM
         * @Date : 2018/6/29 18:48
         * @Param cmd
         * @Return java.lang.Object
         * @Version 1.0
         * @Description :
         **/
        java.lang.Process process = null;
        String result=null;
        try {
            process = Runtime.getRuntime().exec(command);
            ByteArrayOutputStream resultOutStream = new ByteArrayOutputStream();
            InputStream errorInStream = new BufferedInputStream(process.getErrorStream());
            InputStream processInStream = new BufferedInputStream(process.getInputStream());
            int num = 0;
            byte[] bs = new byte[1024];
            while ((num = errorInStream.read(bs)) != -1) {
                resultOutStream.write(bs, 0, num);
            }
            while ((num = processInStream.read(bs)) != -1) {
                resultOutStream.write(bs, 0, num);
            }
             result = new String(resultOutStream.toByteArray());
            System.out.println(result);
            errorInStream.close();
            errorInStream = null;
            processInStream.close();
            processInStream = null;
            resultOutStream.close();
            resultOutStream = null;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (process != null) process.destroy();
            process = null;
        }
        return  result;
    }
}
